/**
 * 
 */
package com.mvc.controller.upgrade;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.FileInfo;
import com.mvc.service.upgrade.FileInfoServiceI;
import com.mvc.utils.FileUpload;
import org.apache.commons.io.FileUtils;
import org.huangqj.consant.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


/**
 * @author qingjie
 *
 */
@RequestMapping("/file")
@Controller
public class FileController {

	@Autowired
	private FileInfoServiceI fis;
	
	Logger logger = LoggerFactory.getLogger(FileController.class);

    @RequestMapping("/upload")
    @ResponseBody
    public Json upload(@RequestParam("pathName") MultipartFile file, HttpServletRequest request, FileInfo fileInfo) throws IOException {
    	Json j = new Json();
    	String newName = "";
    	FileInfo fi = new FileInfo();
    	 if (!file.isEmpty()) {  
         	String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
         	newName = new Date().getTime()+"."+suffix;
         	// 文件保存路径 
         	String filePath = "c:/bxds/upload/"+newName;            
         	fi.setFilename(file.getOriginalFilename());
         	fi.setNewFilename(newName);
         	fi.setPath(filePath);
         	fi.setSize(file.getSize()+"");
         	fi.settId(request.getParameter("tid"));
         	fi.settName(request.getParameter("tName"));
         	fi.setSuffix(suffix);
    	 }
        String fileName = FileUpload.uploadFile(file, request,newName);
        if(!ValidatorUtil.isBlankOnString(fileName)){
        	fileInfo = fis.saveOrUpdate(fi);
        }
        if(fileInfo!=null){
        	j.setMessage("上传成功！");
			j.setSuccess(true);
			j.setObj(fileInfo);
		}else{
			j.setMsg("上传失败");
			j.setSuccess(false);
		}
        return j;

    }

    @RequestMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        try {
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setContentType("image/jpeg; charset=utf-8");
            os.write(FileUtils.readFileToByteArray(FileUpload.getFile(fileName)));
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
 

}
